home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung 2 / Power-Programmierung CD 2 (Tewi)(1994).iso / gnu / gnulib / dkbtrace / pbmplus / source / pbm / pbmtomgr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-08-06  |  2.6 KB  |  122 lines

  1. /* pbmtomgr.c - read a portable bitmap and produce a MGR bitmap
  2. **
  3. ** Copyright (C) 1989 by Jef Poskanzer.
  4. **
  5. ** Permission to use, copy, modify, and distribute this software and its
  6. ** documentation for any purpose and without fee is hereby granted, provided
  7. ** that the above copyright notice appear in all copies and that both that
  8. ** copyright notice and this permission notice appear in supporting
  9. ** documentation.  This software is provided "as is" without express or
  10. ** implied warranty.
  11. */
  12.  
  13. #include "pbm.h"
  14. #include "mgr.h"
  15.  
  16. static void putinit ARGS(( int rows, int cols ));
  17. static void putbit ARGS(( bit b ));
  18. static void putrest ARGS(( void ));
  19. static void putitem ARGS(( void ));
  20.  
  21. void
  22. main( argc, argv )
  23.     int argc;
  24.     char* argv[];
  25.     {
  26.     FILE* ifp;
  27.     bit* bitrow;
  28.     register bit* bP;
  29.     int rows, cols, format, padright, row, col;
  30.  
  31.     pbm_init( &argc, argv );
  32.  
  33.     if ( argc > 2 )
  34.     pm_usage( "[pbmfile]" );
  35.  
  36.     if ( argc == 2 )
  37.     ifp = pm_openr( argv[1] );
  38.     else
  39.     ifp = stdin;
  40.  
  41.     pbm_readpbminit( ifp, &cols, &rows, &format );
  42.     bitrow = pbm_allocrow( cols );
  43.     
  44.     /* Round cols up to the nearest multiple of 8. */
  45.     padright = ( ( cols + 7 ) / 8 ) * 8 - cols;
  46.  
  47.     putinit( rows, cols );
  48.     for ( row = 0; row < rows; ++row )
  49.     {
  50.     pbm_readpbmrow( ifp, bitrow, cols, format );
  51.         for ( col = 0, bP = bitrow; col < cols; ++col, ++bP )
  52.         putbit( *bP );
  53.     for ( col = 0; col < padright; ++col )
  54.         putbit( 0 );
  55.         }
  56.  
  57.     pm_close( ifp );
  58.  
  59.     putrest( );
  60.  
  61.     pm_close (stdout);
  62.  
  63.     exit( 0 );
  64.     }
  65.  
  66. static unsigned char item;
  67. static int bitsperitem, bitshift;
  68.  
  69. static void
  70. putinit( rows, cols )
  71.     int rows, cols;
  72.     {
  73.     struct b_header head;
  74.  
  75.     head.magic[0] = 'y';
  76.     head.magic[1] = 'z';
  77.     head.h_wide = ( ( cols >> 6 ) & 0x3f ) + ' ';
  78.     head.l_wide = ( cols & 0x3f ) + ' ';
  79.     head.h_high = ( ( rows >> 6 ) & 0x3f ) + ' ';
  80.     head.l_high = ( rows & 0x3f ) + ' ';
  81.     head.depth = ( 1 & 0x3f ) + ' ';
  82.     head._reserved = ' ';
  83.     fwrite( &head, sizeof(head), 1, stdout );
  84.  
  85.     item = 0;
  86.     bitsperitem = 0;
  87.     bitshift = 7;
  88.     }
  89.  
  90. #if __STDC__
  91. static void
  92. putbit( bit b )
  93. #else /*__STDC__*/
  94. static void
  95. putbit( b )
  96.     bit b;
  97. #endif /*__STDC__*/
  98.     {
  99.     if ( bitsperitem == 8 )
  100.     putitem( );
  101.     ++bitsperitem;
  102.     if ( b == PBM_BLACK )
  103.     item += 1 << bitshift;
  104.     --bitshift;
  105.     }
  106.  
  107. static void
  108. putrest( )
  109.     {
  110.     if ( bitsperitem > 0 )
  111.     putitem( );
  112.     }
  113.  
  114. static void
  115. putitem( )
  116.     {
  117.     fwrite( &item, sizeof(item), 1, stdout );
  118.     item = 0;
  119.     bitsperitem = 0;
  120.     bitshift = 7;
  121.     }
  122.